package com.gmail.yesguxun.tumblr;

import android.content.Context;
import android.content.SharedPreferences;

public class TrAccount {
	
	private static TrAccount mTrAccount = null;
	
	private Context mAppContext = null;
	private String mEMail = null;
	private String mPassword = null;
	private String mSizeSelection = null;
	private String mViewModeSelection = null;
	private boolean mLoginState = false;
	
	public static TrAccount getTrAccount()
	{
		if(mTrAccount == null)
		{
			mTrAccount = new TrAccount();
		}
		
		return mTrAccount;
	}
	
	public TrAccount()
	{
		
	}
	
	public void setLoginState(boolean loginState)
	{
	   	mLoginState = loginState;
	}
	
	public boolean getLoginState()
	{
		return mLoginState;
	}
	
	public void setContext(Context appContext)
	{
		this.mAppContext = appContext;
	}
	
	public String getPassword()
	{
		return this.mPassword;
	}
	
	public String getEMail()
	{
		return this.mEMail;
	}
	
	public String getSizeSelection()
	{
		return this.mSizeSelection;
	}
	
	public String getViewModeSelection()
	{
	    return this.mViewModeSelection;
	}
	
	public void setEMail(String email)
	{
		if(this.mEMail != null && this.mEMail.compareTo(email) == 0)
		{
			return;
		}
		
		this.mEMail = email;
		setLoginState(false);
	}
	
	public void setPassword(String password)
	{
		if(this.mPassword != null && this.mPassword.compareTo(password) == 0)
		{
			return;
		}
		
		this.mPassword = password;
		setLoginState(false);
	}
	
	public void setSizeSelection(String sizeSelection)
	{
		this.mSizeSelection = sizeSelection;
	}
	
	public void setViewModeSelection(String viewModeSelection)
    {
        this.mViewModeSelection = viewModeSelection;
    }
	
	
	public void save()
	{
		SharedPreferences settings = this.mAppContext.getSharedPreferences(Constant.ACCOUNT_PREFS_FILE, Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = settings.edit();
		editor.putString(Constant.ACCOUNT_PREFS_EMAIL, this.mEMail);
		editor.putString(Constant.ACCOUNT_PREFS_PASSWORD, this.mPassword);
		editor.putString(Constant.ACCOUNT_PREFS_PIC_SIZE, this.mSizeSelection);
		editor.putString(Constant.ACCOUNT_PREFS_VIEW_MODE, this.mViewModeSelection);
		editor.commit();
	}
	
	public void load()
	{
		SharedPreferences settings = this.mAppContext.getSharedPreferences(Constant.ACCOUNT_PREFS_FILE, Context.MODE_PRIVATE);
		this.mEMail = settings.getString(Constant.ACCOUNT_PREFS_EMAIL, "");
		this.mPassword = settings.getString(Constant.ACCOUNT_PREFS_PASSWORD, "");
		this.mSizeSelection = settings.getString(Constant.ACCOUNT_PREFS_PIC_SIZE, Constant.ACCOUNT_PREFS_PIC_SIZE_VALUE_DEFAULT);
		this.mViewModeSelection = settings.getString(Constant.ACCOUNT_PREFS_VIEW_MODE, Constant.ACCOUNT_PREFS_VIEW_MODE_VALUE_DEFAULT);
	}
	
	public boolean isAccountInfoValid()
	{
		if(this.mEMail == null || this.mEMail.equals("") || this.mPassword == null || 
		        this.mPassword.equals("") || this.mAppContext == null || 
		        this.mSizeSelection == null || this.mViewModeSelection == null)
		{
			return false;
		}
		
		return true;
	}
	


}
